<template>
  <div class="container">
    <div class="box">
      <h2>用户注册</h2>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="手机号码" prop="telephone">
          <el-input placeholder="请输入手机号" v-model="form.telephone" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input placeholder="请输入密码" type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
          <el-input placeholder="请输入密码" type="password" v-model="form.password2" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input placeholder="请输入昵称" v-model="form.nickname" />
        </el-form-item>
        <el-form-item label="短信验证码:" prop="authCode">
          <div class="code-box">
            <el-input placeholder="请输入短信验证码" v-model="form.authCode" />
            <CCCountDown @click="onCodeClick" ref="codebtn" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="onSure">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script >
  import CCCountDown from '@/components/common/CCCountDown.vue';
  import { doSmsCode, doRegist } from '@/apis/user';
  import ZZToast from '@/utils/toast';
  export default {
    name: 'Regist',
    components: { CCCountDown },
    data() {
      return {
        form: {
          telephone: '',
          password: '',
          password2: '',
          nickname: '',
          authCode: ''
        },
        rules: {
          telephone: [{
            trigger: 'blur',
            validator: (rule, value, callBack) => {
              if (!value) callBack('请输入手机号');
              if (/^[1][0-9]{10}$/.test(value)) callBack();
              else callBack('手机号码不合法');
            }
          }],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码长度不小于6个字符', trigger: 'blur' }
          ],
          password2: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            {
              trigger: 'blur',
              validator: (rule, value, callBack) => {
                if (this.form.password2 === this.form.password) callBack();
                else callBack('两次输入的密码不一样');
              }
            }
          ],
          nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
          authCode: [
            { required: true, message: '请输入短信验证码', trigger: 'blur' }
          ]
        }
      };
    },

    methods: {
      async onCodeClick() {
        this.rules.telephone[0].validator(null, this.form.telephone, async(msg) => {
          if (msg) return;
          const res = await doSmsCode({ telephone: this.form.telephone });
          if (res.code !== 200) {
            ZZToast.warn(res.message);
          } else {
            this.$refs.codebtn.run();
            this.cache.saveSmsTime();
          }
        });
      },

      async onSure() {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
        if (this.form.password !== this.form.password2) {
          alert('两次输入的密码不一样');
          return;
        }
        const data = { ...this.form };
        data.username = data.telephone;
        delete data.password2;
        const res = await doRegist(data);
        if (res.code !== 200) {
          ZZToast.warn(res.message);
        } else {
          this.toast.success('注册成功');
          this.$router.replace('/login');
        }
      }
    },
    mounted() {
      let t = this.cache.fetchSmsTime();
      if (!t) return;
      t = Math.floor((new Date().getTime() - t) / 1000);
      if (t > 0 && t < 60) this.$refs.codebtn.run(60 - t);
    }
  };
</script>
<style lang="scss" type="text/scss" scoped>
.container {
  background: url(http://121.199.12.118:8083/var/upload/static/login/login.png);
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

$space: 20px;

.box {
  position: absolute;
  top: 230px;
  h2 {
    text-align: center;
    margin-bottom: $space;
  }
  .el-form {
    color: white;
    width: 600px;
    background: white;
    .el-input,
    .login-btn {
      width: 100%;
    }
    .login-btn {
      margin-top: 12px;
    }
  }
}

.code-box {
  @include v-center();
  .el-input {
    margin-right: 12px;
  }
}
</style>
